home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / XCMDs / XCMDTools++ / xcmdWind.cp < prev    next >
Encoding:
Text File  |  1995-10-05  |  6.4 KB  |  276 lines  |  [TEXT/CWIE]

  1. //    © Paul B. Beeken, Work In Progress, 1994-5
  2. //    Knowledge Software Consulting.
  3. //
  4. //    These files are a mindlessly simple wrapper class for the
  5. //    basic XCMD operations.  Only one instance of the xcmdBase class is
  6. //    generated per XCMD call but there may be many instances of the XCMDString
  7. //    class.  I have used these classes to whip out XCMD/XFCNs within hours of
  8. //    receiving the specs.  They work great for me but I will always consider 
  9. //    suggestions.
  10. //
  11. //    Please, please, please, in the unlikely event you should use this stuff
  12. //    for some commercial application I would appreciate you contacting me.  If
  13. //    its for your own use, use away. Send email: knowsoft@ios.com
  14. //
  15. //    As always: this file is presented as is with no warrantees expressed or implied.
  16. //    Swim at your own risk, etc. etc.
  17.  
  18. #include "xcmdWind.h"
  19.  
  20. xcmdWindow::xcmdWindow( XCmdPtr xP, Boolean lockParameters ) : 
  21.     xcmdBase( xP, lockParameters )     // set up xcmdBase and continue...
  22. {
  23.  
  24.     if ( nParams() < 0 ) {    // This entry involves an "event"
  25.  
  26.         xEventInfo    = XWEventInfoPtr( (*this)[0] );        // Get ext param block
  27.         xEvent        = xEventInfo->event;                // Copy eventRecord
  28.         xPrivStore    = (Handle)GetWRefCon( (WindowPtr)getXWindow() );
  29.         xWindow        = (CWindowPtr)xEventInfo->eventWindow; // We are always color
  30.  
  31.         GetPort( &oldXPort );                            // keep old port and...
  32.         SetPort( (WindowPtr)xWindow );                                // Setup new one.
  33.  
  34.         passMsg( true );    // the buck doesn't stop here
  35.  
  36.         }
  37.         
  38.     else {                    // plain jane call.
  39.  
  40.         xEventInfo    = nil;
  41.         xPrivStore    = nil;
  42.         oldXPort    = nil;
  43.         xWindow        = nil;
  44.         }
  45.  
  46. }
  47.  
  48. xcmdWindow::~xcmdWindow( void )
  49. {
  50.     if ( isWindowEvent() ) { // We were called through an "event" 
  51.         SetWRefCon( (WindowPtr)getXWindow(), (long)xPrivStore ); // We may have changed the handle.
  52.         SetPort( oldXPort );        // restore old environment
  53.         } // if
  54. }
  55.  
  56. CWindowPtr
  57. xcmdWindow::createWindow( short rID, ResType rType, Boolean floating )
  58. {
  59.     // get from a template.  (always a color window)
  60.     return (CWindowPtr) 
  61.         GetNewXWindow( theParamPtr(), rType, rID, true, floating );
  62. }
  63.  
  64. void
  65. xcmdWindow::doWindowEvent( void )
  66. {
  67.     if ( !isWindowEvent() ) return;
  68.  
  69.     switch( xEvent.what ) {
  70.         case xOpenEvt:            /* the first event after you are created */
  71.                 doOpenWindow();
  72.                 break;
  73.         case xCloseEvt:            /* your window is being forced close (Quit?) */
  74.                 doCloseWindow();
  75.                 break;
  76.  
  77.         // We only get these calls if we made some special call to a call back.
  78.         case xGiveUpEditEvt:    /* you are losing Edit... */
  79.         case xEditUndo:            /* Edit——Undo */
  80.         case xEditCut:            /* Edit——Cut */
  81.         case xEditCopy:            /* Edit——Copy */
  82.         case xEditPaste:        /* Edit——Paste */
  83.         case xEditClear:        /* Edit——Clear */
  84.                 doEditEvent( xEvent.what );
  85.                 break;
  86.  
  87.         //case xGiveUpSoundEvt:    /* you are losing the sound channel... */
  88.                 // homey don't play this.
  89.  
  90.         case xMBarClickedEvt:    /* a menu is about to be shown--update if needed */
  91.                 doMenuUpdate();
  92.                 break;
  93.         case xMenuEvt:            /* user has selected an item in your menu */
  94.                 doMenuClick( long( getXEventParam(1) ), long( getXEventParam(2) ) );
  95.                 break;
  96.                 
  97.         // These are not documented well.
  98.         case xHidePalettesEvt:    /* someone called HideHCPalettes */
  99.         case xShowPalettesEvt:    /* someone called ShowHCPalettes */
  100.                 ShowHide( (WindowPtr)getXWindow(), (xEvent.what==xShowPalettesEvt));
  101.                 passMsg( false );
  102.                 break;
  103.  
  104.         case xSendEvt:            /* script has sent you a message (text) */
  105.                 doMessage( StringPtr( getXEventParam(0) ) );
  106.                 break;
  107.  
  108.         case xSetPropEvt:        /* set a window property */
  109.                 doSetProperty( StringPtr( getXEventParam(0) ), StringPtr( getXEventParam(1) ) );
  110.                 break;
  111.         case xGetPropEvt:        /* get a window property */
  112.                 xcmdString* rs = doGetProperty( StringPtr( getXEventParam(0) ) );
  113.                 setXEventResult( rs );
  114.                 if ( rs ) delete rs;
  115.                 break;
  116.  
  117.         case xCursorWithin:        /* cursor is within the window */
  118.                 doSetCursor();
  119.                 break;
  120.  
  121. // The standard toolbox events
  122.         case nullEvent:            /* only gets called if we have "interupt" code */
  123.                 //doIdleStuff();
  124.                 break;
  125.  
  126.         case activateEvt:        /* window is bcoming active */
  127.                 doAcitveWindow( xEvent.modifiers & activeFlag );
  128.                 break;
  129.  
  130.         case updateEvt:
  131.                 doUpdateWindow();
  132.                 passMsg( false );
  133.                 break;
  134.  
  135.         case app4Evt:            /* suspend/resume */
  136.                 ShowHide( (WindowPtr)getXWindow(), (xEvent.message % 2 != 0));
  137.                 passMsg( false );
  138.                 break;
  139.         } // switch
  140. }
  141.  
  142. void
  143. xcmdWindow::doAcitveWindow( Boolean activating )
  144. {    
  145.     // an activate/deactivate event.
  146.  
  147.     if ( activating ) {
  148.         ; // do some activating
  149.     } else {
  150.         ; // do some deactivating
  151.     }
  152.     
  153. }
  154.  
  155. void
  156. xcmdWindow::doUpdateWindow( void )
  157. {
  158.     ::BeginUpdate((WindowPtr)getXWindow());
  159.     
  160.         doDrawWindow();
  161.                 
  162.     ::EndUpdate((WindowPtr)getXWindow());
  163. }
  164.  
  165. // These are the default actions for the window. most do nothing.
  166. void
  167. xcmdWindow::doOpenWindow( void )
  168. {
  169.     // Window is openning, initialize
  170.     
  171.     // Show it.
  172.     ShowHide( (WindowPtr)getXWindow(), true );
  173.     
  174. }
  175.  
  176. void
  177. xcmdWindow::doCloseWindow( void )
  178. {
  179.     // Window is closing, clean up
  180.     passMsg( true ); // go ahead, close it.
  181. }
  182.  
  183. void
  184. xcmdWindow::doEditEvent( short what )
  185. {
  186.     // Handle events relating to editing.
  187.     switch( what ) {
  188.             case xGiveUpEditEvt:    /* you are losing Edit... */
  189.             case xEditUndo:            /* Edit——Undo */
  190.             case xEditCut:            /* Edit——Cut */
  191.             case xEditCopy:            /* Edit——Copy */
  192.             case xEditPaste:        /* Edit——Paste */
  193.             case xEditClear:        /* Edit——Clear */
  194.             ;
  195.         }
  196. }
  197.  
  198.  
  199. void
  200. xcmdWindow::doMenuUpdate( void )
  201. {
  202.     // A menuBar click, set up menus
  203. }
  204.  
  205. void
  206. xcmdWindow::doMenuClick( long m, long i )
  207. {
  208.     // A menu click
  209. }
  210.  
  211.         
  212. void
  213. xcmdWindow::doMessage( const xcmdString& msg )
  214. {
  215.     // a send message to window...
  216. }
  217.  
  218. void
  219. xcmdWindow::doDrawWindow( void )
  220. {
  221.     // do some drawing.
  222. }
  223.         
  224. void
  225. xcmdWindow::doSetProperty( const xcmdString& prop, const xcmdString& val )
  226. {
  227.     // set a window property
  228.     passMsg( true );  // let hypercard handle it
  229. }
  230.  
  231. xcmdString*
  232. xcmdWindow::doGetProperty( const xcmdString& prop )
  233. {
  234.     // get a window property
  235.     passMsg( true );  // let hypercard handle it
  236.     return nil;
  237. }
  238.  
  239. void
  240. xcmdWindow::doSetCursor( void )
  241. {
  242.     // cursor is within the window */
  243.     passMsg( false );  // let hypercard handle it
  244. }
  245.  
  246. // Accessors
  247.  
  248.         // These functions must be used with great care.
  249.         // I don't check to see if the xEvent pointer is valid.
  250. CWindowPtr
  251. xcmdWindow::getXWindow( void )
  252. {
  253.     return xWindow;
  254. }
  255.  
  256. void*
  257. xcmdWindow::getXEventParam( int i )
  258. {
  259.     return (void*) xEventInfo->eventParams[i];
  260. }
  261.  
  262. void
  263. xcmdWindow::setXEventResult( xcmdString* rv )
  264. {
  265.     if ( rv ) 
  266.         xEventInfo->eventResult = Handle(*rv);
  267.     else
  268.         xEventInfo->eventResult = nil;  // nil
  269. }
  270.  
  271. EventRecord*
  272. xcmdWindow::getXEvent( void )
  273. {
  274.     return &xEvent;
  275. }
  276.